<template>
  <el-breadcrumb class="app-breadcrumb" separator="">
    <transition-group name="breadcrumb">
      <el-breadcrumb-item v-for="(item) in levelList" :key="item.path">
        <a
          v-if="item.redirect === 'noRedirect' || index == levelList.length - 1"
          :data-index="index"
          class="no-redirect"
          @click.prevent="handleLink(item, index)"
        >{{ item.meta.title }}</a>
        <a
          v-else
          :data-index="index"
          @click.prevent="handleLink(item, index)"
        >{{ item.meta.title }}</a>
      </el-breadcrumb-item>
    </transition-group>
  </el-breadcrumb>
</template>

<script>
import pathToRegexp from 'path-to-regexp'

export default {
  data() {
    return {
      levelList: [{
        path: '/dashboard',
        redirect: '/dashboard',
        meta: { title: '工作台', icon: 'dashboard' }

      }],
      index: null
    }
  },
  watch: {
    $route() {
      this.getBreadcrumb()
    }
  },
  created() {
    this.getBreadcrumb()
  },
  methods: {
    // tagBar
    getBreadcrumb() {
      // const tagList = document.querySelectorAll('[data-index]')
      // tagList.forEach((item) => {
      //   item.classList.remove('no-redirect')
      // })
      // this.$nextTick(() => {
      //   const arr = document.querySelectorAll('.no-redirect')
      //   console.log(arr)
      //   if (!arr) {
      //     this.handleLink(this.index)
      //   }
      //   console.log(this.index)
      // })
      // only show routes with meta.title
      const matched = this.$route.matched.filter(
        (item) => item.meta && item.meta.title
      )
      // console.log(matched)
      let first = ''
      if (matched.length > 1) {
        first = matched[1]
      } else {
        first = matched[0]
      }

      if (!this.levelList.includes(first) && first.meta.title !== '工作台') {
        setTimeout(() => {
          this.levelList.push(first)
        })
      }
    },

    pathCompile(path) {
      const { params } = this.$route
      var toPath = pathToRegexp.compile(path)
      return toPath(params)
    },
    handleLink(item = '', index) {
      // console.log(index)
      const { redirect, path } = item
      if (redirect) {
        this.$router.push(redirect)
        return
      }
      setTimeout(() => {
        const tagList = document.querySelectorAll('[data-index]')
        tagList.forEach((item) => {
          item.classList.remove('no-redirect')
        })
        document
          .querySelector(`[data-index='${index}']`)
          .classList.add('no-redirect')
      })
      // console.log(document.querySelector(`[data-index='${index}']`))
      // console.log(1)
      // console.log(index)
      this.$router.push(this.pathCompile(path))
    },
    clearAll() {
      this.levelList = [{
        path: '/dashboard',
        redirect: '/dashboard',
        meta: { title: '工作台', icon: 'dashboard' }

      }]
    }
  }
}
</script>

<style lang="scss" scoped>
.app-breadcrumb.el-breadcrumb {
  display: inline-block;
  font-size: 14px;
  height: 50px;
  line-height: 50px;;
   padding-left: 25px ;
  .no-redirect {
    color: #ffb200;

  }
}
::v-deep .el-breadcrumb__item{
  background: #fff;
  border-radius: 6px;
  width: 120px;
  margin: 0 5px;
  text-align: center;
}
::v-deep.el-breadcrumb__item:last-child .el-breadcrumb__inner,
.el-breadcrumb__item:last-child .el-breadcrumb__inner a, .el-breadcrumb__item:last-child .el-breadcrumb__inner a:hover,
 .el-breadcrumb__item:last-child .el-breadcrumb__inner:hover{
cursor:pointer
 }
</style>
